/**
 *	@file	d_iwast.c
 *	@brief	デバッグ用岩澤常駐部ソース
 *	@author	Miyuki Iwasawa
 *	@date	06.06.01
 */

#ifdef PM_DEBUG

#include "common.h"
#include "fld_bmp.h"
#include "fntsys.h"
#include "talk_msg.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/brightness.h"
#include "system/fontproc.h"
#include "system/wipe.h"
#include "system/pm_overlay.h"
#include "gflib/strbuf.h"
#include "gflib/strbuf_family.h"
#include "field/fieldsys.h"
#include "field/field.h"
#include "field/ev_mapchange.h"
#include "field/fieldmap.h"
#include "field/zonedata.h"
#include "field/field_event.h"
#include "field/field_matrix.h"
#include "fielddata/maptable/zone_id.h"
#include "application/mailtool.h"
#include "script.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_debug_miyuki.h"

#include "field/d_iwa.h"

//プロトタイプ
BOOL D_WazaOshieEvent( GMEVENT_CONTROL * event );
static void DebugMiyukiManager(TCB_PTR tcb,void* work);

FS_EXTERN_OVERLAY(debug_iwasawa);

/**
 *	デバッグシステム初期化
 *
 *	u8	DebugMiyukiInit(void* fsys)
 *
 *	@param	void* FLD_DEB_DATへのポインタ
 *	@return none
 */
void DebugMiyukiInit(void* fsys)
{
	int* buf = sys_AllocMemory(HEAPID_BASE_DEBUG,sizeof(int));
	*buf = 0;

	//オーバーレイロード
	Overlay_Load( FS_OVERLAY_ID( debug_iwasawa ), OVERLAY_LOAD_NOT_SYNCHRONIZE );

	DebugMiyukiStart(fsys,buf);

	//終了監視タスクを立ち上げ
	TCB_Add(DebugMiyukiManager,buf,1);
}

//=============================================================================
/**
 *	デバッグシステム終了監視
 *	void DebugMiyukiMain(u8 id);
 *
 *	@param	tcb	TCB_PTR	タスクブロックポインタ
 *	@param	work	タスクワークポインタ
 *	@return	none
 */
//=============================================================================
static void DebugMiyukiManager(TCB_PTR tcb,void* work)
{
	int* end_f = (int*)work;
	if(*end_f == FALSE){
		return;
	}
	sys_FreeMemoryEz(work);
	TCB_Delete(tcb);

	//オーバーレイ解放
	Overlay_UnloadID( FS_OVERLAY_ID( debug_iwasawa ) );
}

void DebugMiyukiFuncCall(TCB_PTR tcb,void* work)
{
	_DEBUG_IWA_WORK	*wp = (_DEBUG_IWA_WORK*)work;
	PGEAR_PARAM	*pwork;

	switch(wp->seq){
	case DMSEQ_FADEOUT:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEOUT,
				WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_BASE_DEBUG
				);

		wp->seq = DMSEQ_FADEOUTWAIT;
		break;
	case DMSEQ_FADEOUTWAIT:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		DebugMiyukiViewEnd(wp);
		//ファンクション実行、プロセス登録
		{
			void (*dproc_call)(void*);
			dproc_call = wp->UserFunc;
			(*dproc_call)(wp);
			FieldSystemProc_SeqHoldEnd();
		}
		wp->seq = DMSEQ_FUNC;
		break;
	case DMSEQ_FUNC:
		//プロセス終了待ち
		if(FieldEvent_Cmd_WaitSubProcEnd(wp->fsys)){
			break;
		}
		//メモリを確保していれば解放
		if(wp->subWork != NULL){
			switch(wp->func_mode){
			case DM_FMODE_PGEAR:
				pwork = (PGEAR_PARAM*)wp->subWork;
				if(pwork->retval){
					wp->ret_x = pwork->ret_x;
					wp->ret_z = pwork->ret_z;
					wp->ret_zone = pwork->ret_zone;
					wp->ret_val = pwork->retval;
					wp->seq = DMSEQ_JUMPIN;
				}else{
					wp->ret_x = 0;
					wp->ret_z = 0;
					wp->seq = DMSEQ_FADEIN;
				}
				sys_FreeMemoryEz(wp->subWork);
				break;
			case DM_FMODE_DEFAULT:
			default:
				wp->seq = DMSEQ_FADEIN;
				sys_FreeMemoryEz(wp->subWork);
				break;
			}
			wp->subWork = NULL;
		}else{
			wp->seq = DMSEQ_FADEIN;
		}
		//フィールドマッププロセス復帰
		FieldEvent_Cmd_SetMapProc(wp->fsys);
		break;
	case DMSEQ_FADEIN:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(wp->fsys)){
			//デバッグメニュー復帰
			DebugMiyukiViewInit(wp,wp->fsys,wp->List,wp->Cursor);
			FieldSystemProc_SeqHold();
			//ブラックインリクエスト
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_BASE_DEBUG
				);
			wp->seq = DMSEQ_FADEINWAIT;
			break;
		}
		break;
	case DMSEQ_FADEINWAIT:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		FieldSystemProc_SeqHold();
		TCB_ChangeFunc(tcb,DebugMiyukiMain);
		wp->seq = DMSEQ_FADEOUT;
		break;
	case DMSEQ_JUMPIN:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(wp->fsys)){
			FieldSystemProc_SeqHold();
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_BASE_DEBUG
				);
			GF_BGL_ScrFill(wp->pBglIni,GF_BGL_FRAME3_M,0x0000,0,0,32,32,GF_BGL_SCRWRT_PALIN);
			GF_BGL_LoadScreenV_Req(wp->pBglIni,GF_BGL_FRAME3_M);
			wp->seq = DMSEQ_JUMPSET;
			break;
		}
		break;
	case DMSEQ_JUMPSET:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		EventSet_EasyMapChange(wp->fsys,
			wp->ret_zone,DOOR_ID_JUMP_CODE,
			wp->ret_x*32+16,wp->ret_z*32+16, DIR_DOWN);
		FieldSystemProc_SeqHold();
		wp->seq = DMSEQ_JUMPWAIT;
		break;
	case DMSEQ_JUMPWAIT:
		*(wp->tcb_end_f) = TRUE;

		//デバッグワークメモリ開放
		sys_FreeMemoryEz(work);
		TCB_Delete(tcb);

		//OBJ等の動作停止解除
		FieldSystemProc_SeqHoldEnd();
		break;
	}
}

//デバッグメールボックス終了待ち
void DebugMiyukiSubMenuWait(TCB_PTR tcb,void* work)
{
	_DEBUG_IWA_WORK	*wp = (_DEBUG_IWA_WORK*)work;

	if(!wp->ret_val){
		return;
	}
	wp->ret_val = 0;
	//デバッグメニュー復帰
	DebugMiyukiViewInit(wp,wp->fsys,wp->List,wp->Cursor);
	TCB_ChangeFunc(tcb,DebugMiyukiMain);
}

BOOL D_WazaOshieEvent( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	D_WO_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		if( WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			FieldOBJSys_MoveStopAll( fsys->fldobjsys );
			WazaOshie_ProcSet( fsys , &wk->dat );
			wk->seq = 1;
		}
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			FieldEvent_Cmd_SetMapProc( fsys );
			wk->seq = 2;
		}
		break;

	case 2:
		if( FieldEvent_Cmd_WaitMapProcStart( fsys ) ){
			FieldFadeWipeSet( FLD_DISP_BRIGHT_BLACKIN );
			wk->seq = 3;
		}
		break;

	case 3:
		if( WIPE_SYS_EndCheck() ){
			if( wk->dat.mode == WAZAOSHIE_MODE_REMIND ){
				sys_FreeMemoryEz( wk->dat.waza_tbl );
			}
			sys_FreeMemoryEz( wk );
			FieldOBJSys_MoveStopAllClear( fsys->fldobjsys );
			return TRUE;
		}
	}
	return FALSE;
}

#endif	//ifdef PM_DEBUG
